<?php
/**
 * User:于小涛
 * Date:2020/2/17
 * Time:15:37
 */
namespace app\admin\controller\system;

use app\admin\BaseController;
use app\admin\model\rbac\Admin;
use app\admin\model\system\Channel as ChannelModel;
use app\admin\model\system\ChannelBindAdmin;
use think\App;
use think\facade\View;

class Channel extends BaseController {
    private $obj_channel_model = null;
    private $obj_admin_model = null;
    private $obj_channel_bind_admin_model = null;
    private $arr_tuiguangzhuanyuan_list=[];
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->obj_channel_model = new ChannelModel();
        $this->obj_channel_bind_admin_model = new ChannelBindAdmin();
        $this->obj_admin_model = new Admin();
        $int_role_tuiguangzhuanyuan_id=config('project.role_tuiguangzhuanyuan_id');
        $str_sql="select ra.* from rbac_admin_role as rar left join rbac_admin as ra on ra.id=rar.admin_id where ra.is_lock=1 and ra.is_del=1 and rar.role_id={$int_role_tuiguangzhuanyuan_id}";
        $this->arr_tuiguangzhuanyuan_list=$this->obj_admin_model->query($str_sql);
        foreach($this->arr_tuiguangzhuanyuan_list as &$arr_item){
            $arr_item['value'] = $arr_item['id'];
        }
        View::assign('arr_tuiguangzhuanyuan_ids',$this->arr_tuiguangzhuanyuan_list);
    }

    public function getList(){
        return view();
    }

    public function getListData(){
        $arr_list=$this->obj_channel_model
            ->where('name','like',"%{$this->request->param('name')}%")
            ->order("sort","desc")
            ->paginate(['list_rows'=>$this->request->param('limit'),'page'=>$this->request->param('page')])
            ->toArray();
        foreach ($arr_list['data'] as &$arr_item){
            $arr_item['admin_names'] = '';
            $str_sql="select ra.* from channel_bind_admin as cba left join rbac_admin as ra on ra.id=cba.admin_id where cba.channel_id={$arr_item['id']}";
            $arr_data=$this->obj_channel_bind_admin_model->query($str_sql);
            foreach($arr_data as $arr_item_bind){
                $arr_item['admin_names'].=$arr_item_bind['name'].'、';
            }
        }
        return output_page($arr_list);
    }

    public function add(){
        if($this->request->param('name')){
            $arr_data=[
                'name'=>$this->request->param('name'),
                'sort'=>$this->request->param('sort'),
            ];
            $arr_re=$this->obj_channel_model->add($arr_data);
            $int_channel_id=$this->obj_channel_model->getLastInsID();
            if($arr_re['ret']==0){
                op_log('system','add',"创建渠道成功，名称【{$this->request->param('name')}】");
                //绑定推广专员
                if(!empty($this->request->param('admin_ids'))){
                    $arr_admin_ids=explode(',',$this->request->param('admin_ids'));
                    foreach($arr_admin_ids as $int_admin_id){
                        $arr_add_data=[
                            'admin_id'=>$int_admin_id,
                            'channel_id'=>$int_channel_id
                        ];
                        $int_num=$this->obj_channel_bind_admin_model->add($arr_add_data);
                    }
                }
                return output_data();
            }else{
                return output_data(array(),$arr_re['reason']);
            }
        }else{
            return view();
        }
    }

    public function edit(){
        if($this->request->param('name')){
            $arr_data=[
                'name'=>$this->request->param('name'),
                'sort'=>$this->request->param('sort'),
            ];
            $arr_re=$this->obj_channel_model->edit($this->request->param('id'),$arr_data);
            if($arr_re['ret']==0){
                op_log('system','edit',"修改渠道成功，名称【{$this->request->param('name')}】");
                //删除所有的绑定
                $arr_del_bind_where=[
                    'channel_id'=>$this->request->param('id')
                ];
                $int_del_re=$this->obj_channel_bind_admin_model->where($arr_del_bind_where)->delete();
                if(!empty($this->request->param('admin_ids'))){
                    $arr_admin_ids=explode(',',$this->request->param('admin_ids'));
                    foreach($arr_admin_ids as $int_admin_id){
                        $arr_add_data=[
                            'admin_id'=>$int_admin_id,
                            'channel_id'=>$this->request->param('id')
                        ];
                        $int_num=$this->obj_channel_bind_admin_model->add($arr_add_data);
                    }
                }
                return output_data();
            }else{
                return output_data(array(),$arr_re['reason']);
            }
        }else{
            $arr_data_info=$this->obj_channel_model->findOrEmpty($this->request->param('id'))->toArray();
            //获取每个渠道绑定的推广专员
            $arr_where=[
                'channel_id'=>$arr_data_info['id']
            ];
            $arr_channel_bind_admin_list=$this->obj_channel_bind_admin_model->where($arr_where)->select()->toArray();
            $arr_admin_ids=[];
            foreach($arr_channel_bind_admin_list as $arr_bind_item){
                $arr_admin_ids[]=$arr_bind_item['admin_id'];
            }
            $arr_data_info['admin_ids'] = implode(',',$arr_admin_ids);
            View::assign('arr_data_info',$arr_data_info);
            return view();
        }
    }

    public function del(){
        //先检查是否绑定了管理员
        $arr_where_check=[
            'channel_id'=>$this->request->param('id')
        ];
        $arr_bind_admin_list=$this->obj_channel_bind_admin_model->where($arr_where_check)->select()->toArray();
        if(!empty($arr_bind_admin_list)){
            return output_data(array(),'error','请先删除该渠道下的管理员');
        }
        $arr_re=$this->obj_channel_model->del($this->request->param('id'));
        if($arr_re['ret']==0){
            op_log('system','del',"删除渠道成功");
            return output_data();
        }else{
            return output_data(array(),'error',$arr_re['reason']);
        }
    }
}